<?php

class ValidationModel {

	const MemberName = "name";
	const MemberSSN = "ssn";
	const MemberPassword = "password";

	const BoatLenght = "length";

	const NameREGEXP = '/^(?=.{1,15}$)[a-zA-Z][a-zA-Z0-9]*(?: [a-zA-Z0-9]+)*$/';
	const SSNREGEXP = '/^[0-9]{10}/';
	const PassREGEXP = '/^(?=.{1,15}$)[a-zA-Z][a-zA-Z0-9]*(?: [a-zA-Z0-9]+)*$/';

	/**
	 * Will validate memberInfo, if not valid it will set a message
	 * @access public
	 * @param array memberInfo
	 * @return bool
	 */
	public static function validateMemberInfo($a_memberInfo) {
		$returnBool = true;

		if (preg_match(self::NameREGEXP, $a_memberInfo[self::MemberName]) === false) {
			MessageModel::addMessage("Namnet är inte giltigt!");
			$returnBool = false;
		}
		if (self::isValidSSN($a_memberInfo[self::MemberSSN]) === false) {
			MessageModel::addMessage("Personnumret är inte giltigt!");
			$returnBool = false;
		}
		if (preg_match(self::PassREGEXP, $a_memberInfo[self::MemberPassword]) === false) {
			$this::addMessage("Lösenordet är inte giltigt!");
			$returnBool = false;
		}

		return $returnBool;
	}

	/**
	 * Will validate boatInfo, if not valid it will set a message
	 * @access public
	 * @param length boatinfo
	 * @return bool
	 */
	public static function validateBoatInfo($a_boatInfo) {
		return TRUE ;
		$returnBool = false;

		if (intval($a_boatInfo[self::BoatLenght]) === false) {
			MessageModel::addMessage("Längden måste anges som ett heltal i centimeter!");
			$returnBool = false;
		}

		return $returnBool;
	}

	/**
	 * validates a SSN against a REGEXP and runs it trough calc controlldigit
	 * @access private
	 * @param -
	 * @return bool
	 */
	private static function isValidSSN($a_ssn) {

		//Check against regexp
		if (preg_match(self::SSNREGEXP, $a_ssn) == 0) {
			return false;
		} else {
			//Calc controll digit, if ok return number else return false
			if (self::calcControllDigit($a_ssn) === true) {
				return true;
			} else {
				return false;
			}

		}
	}

	/**
	 * Calculates the last 4 digits to be a ok SSN
	 * @access private
	 * @param SSN
	 * @return bool
	 */
	private static function calcControllDigit($a_ssn) {
		$sum = 0;

		for ($i = 0; $i < strlen($a_ssn) - 1; $i++) {
			$tmp = substr($a_ssn, $i, 1) * (2 - ($i & 1));
			//växla mellan 212121212
			if ($tmp > 9)
				$tmp -= 9;
			$sum += $tmp;
		}

		//extrahera en-talet
		$sum = (10 - ($sum % 10)) % 10;

		return substr($a_ssn, -1, 1) == $sum;
	}

	/**
	 * Turns a SSN into format xxxxxxxxxx
	 * @access public
	 * @param SSN
	 * @return trimmed SSN or null if not valid format given
	 */
	public static function trimSSN($a_ssn) {
		$a_ssn = trim($a_ssn);

		switch (strlen($a_ssn)) {
			case '13' :
			/*19780517-xxxx */
				$a_ssn = str_replace("-", "", $a_ssn);
				$a_ssn = substr($a_ssn, 2);
				//Now we have 780517xxxx
				break;

			case '12' :
			/*19780517xxxx */
				$a_ssn = substr($a_ssn, 2);
				break;

			case '11' :
			/*780517-xxxx */
				$a_ssn = str_replace("-", "", $a_ssn);
				break;

			case '10' :
			/*780517xxxx */
			//Number is good, keep on rocking...
				break;

			default :
				return null;
				break;
		}

		return $a_ssn;
	}

}
